<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <!-- 搜索 clearable:清空数据，@clear:查询数据-->
        <el-input v-model="xx" placeholder="输入学号进行查询，查询结果为用户输入的学生"
          ><template #append>
            <el-button @click="getls()"
              ><el-icon><Search /></el-icon
            ></el-button>
          </template> </el-input
      ></el-col>
      <!--------------------------- 添加用户 -------------------------------->
      <el-col :span="4"><el-button type="primary" @click="addDialogVisible = true">添加用户</el-button> </el-col>
      <el-dialog title="添加用户" v-model="addDialogVisible" width="50%">
        <el-form label-width="70px">
          <el-form-item label="学号">
            <el-input v-model="sno" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="姓名">
            <el-input v-model="name" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="密码">
            <el-input v-model="password" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="级别">
            <el-input v-model="level" placeholder="请输入0/1"></el-input>
          </el-form-item>

          <el-form-item label="性别">
            <el-input v-model="gender" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="年龄">
            <el-input v-model="age" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="系别">
            <el-input v-model="dept" placeholder="请输入"></el-input>
          </el-form-item>

          <el-form-item label="班级">
            <el-input v-model="class_sql" placeholder="请输入"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="insertstudent()">确 定</el-button>
        </span>
      </el-dialog>
    </el-row>

    <el-table :data="user">
      <el-table-column type="index"></el-table-column>

      <el-tableColumn label="学号" prop="sno"></el-tableColumn>
      <el-tableColumn label="姓名" prop="name"></el-tableColumn>
      <el-tableColumn label="密码" prop="password"></el-tableColumn>
      <el-tableColumn label="级别" prop="level"></el-tableColumn>
      <el-tableColumn label="性别" prop="gender"></el-tableColumn>
      <el-tableColumn label="年龄" prop="age"></el-tableColumn>
      <el-tableColumn label="系别" prop="dept"></el-tableColumn>
      <el-tableColumn label="班级" prop="class"></el-tableColumn>

      <el-tableColumn label="操作" width="180px">
        <template v-slot="scope">
          <el-button round type="primary" size="mini" @click="editDialogVisible = true"
            ><el-icon><Edit /></el-icon
          ></el-button>

          <el-button round type="danger" size="mini" @click="del(scope.row)"
            ><el-icon><Delete /></el-icon
          ></el-button>
        </template>
      </el-tableColumn>
    </el-table>

    <div>学生资料查询区</div>
    <el-button type="primary" @click="getallstudent()">所有学生信息</el-button>
		<el-table
			:data="test_studentlist"
			border
			stripe
		>
			<!--指定数据源!-->
			<el-table-column
				label="学号"
				prop="sno"
			>
			</el-table-column>

			<el-table-column
				label="姓名"
				prop="name"
			>
			</el-table-column>

			<el-table-column
				label="密码"
				prop="password"
			>
			</el-table-column>

			<el-table-column
				label="等级"
				prop="level"
			>
			</el-table-column>

			<el-table-column
				label="性别"
				prop="gender"
			>
			</el-table-column>
				
			<el-table-column
				label="年龄"
				prop="age"
			>
			</el-table-column>
				
			<el-table-column
				label="系别"
				prop="dept"
			>
			</el-table-column>
				
			<el-table-column
				label="班级"
				prop="class"
			>
			</el-table-column>
		</el-table>

    <el-dialog title="修改用户资料" v-model="editDialogVisible" width="50%">
      <!-- 添加用户内容底部区域 -->
      <el-form label-width="70px">
        <el-form-item label="学号">
          <el-input v-model="updatesno" placeholder="您要修改的该学生学号"></el-input>
        </el-form-item>

        <el-form-item label="姓名">
          <el-input v-model="updatename" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="密码">
          <el-input v-model="updatepassword" placeholder="请输入"></el-input>
        </el-form-item>

         <el-form-item label="级别">
          <el-input v-model="updatelevel" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="性别">
          <el-input v-model="updategender" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="年龄">
          <el-input v-model="updateage" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="系别">
          <el-input v-model="updatedept" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="班级">
          <el-input v-model="updateclass_sql" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="editDialogVisible = false">取 消</el-button>
        <el-button round type="primary" @click="updatestudent()">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="删除用户资料" v-model="deleteDialogVisible" width="50%">
      <!-- 添加用户内容底部区域 -->
      <el-form label-width="70px">
        <el-form-item label="学号">
          <el-input v-model="deletesno" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="deleteDialogVisible = false">取 消</el-button>
        <el-button round type="primary" @click="deletestudent(e)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { h } from 'vue'
import { ElNotification } from 'element-plus'
import { ref } from 'vue'
export default {
  data() {
    return {
      sno: ref(''),
      name: ref(''),
      password: ref(''),
      level: ref(''),
      gender: ref(''),
      age: ref(''),
      dept: ref(''),
      class_sql: ref(''),
     
      updatesno: ref(''),
      updatename: ref(''),
      updatepassword: ref(''),
      updatelevel: ref(''),
      updategender: ref(''),
      updatage: ref(''),
      updatedept: ref(''),
      updateclass_sql: ref(''),
    
      deletesno: ref(''),

      addDialogVisible: false,
      editDialogVisible: false,
      deleteDialogVisible: false,
      user: [],
      xx: ref(''),
    }
  },
  created() {
    this.getls()
  },
  methods: {
    // 获取用户列表数据（getUserList）
    getls() {
      const that = this
      this.$axios.get('/getstudent', { params: { xx: this.xx } }).then(function (response) {
        console.log(response)
        that.user.push(response['data'])
      })
    },
    del(row) {
      console.log(row)
      this.deleteDialogVisible = true
    },
    addDialogClosed() {
      this.$refs.addUserFormRef.resetFields()
    },
    editDialogClosed() {
      this.$refs.editUserFormRef.resetFields()
    },
    deleteDialogClosed() {
      this.$refs.editUserFormRef.resetFields()
    },
    insertstudent() {
      const that = this
      this.$axios
        .get('/insertstudent', {
          params: {
            sno: this.sno,
            name: this.name,
            password: this.password,
            level: this.level,
            gender: this.gender,
            age: this.age,
            dept: this.dept,
            class_sql: this.class_sql,         
          },
        })
        .then(function (response) {
          that.addDialogVisible = false
          that.$message({
            message: h('i', { style: 'color: coral' }, '用户已插入数据库'),
          })
        })
    },
    updatestudent() {
      const that = this
      this.$axios
        .get('/updatestudent', {
          params: {
            sno: this.updatesno,
            name: this.updatename,
            password: this.updatepassword,
            level: this.updatelevel,
            gender: this.updategender,
            age: this.updateage,
            dept: this.updatedept,
            class_sql: this.updateclass_sql,          
          },
        })
        .then(function (response) {
          that.$message({
            message: '用户已更新',
          })
        })
    },
    deletestudent(e) {
      console.log(e)
      const that = this
      this.$axios
        .get('/deletestudent', {
          params: {
            sid: this.deletesno,
          },
        })
        .then(function (response) {
          that.$message({
            type: 'success',
            message: '用户已在数据库删除',
          })
        })
    },
  },
}
</script>

<style lang="less" scoped></style>
